package com.aegis.spring;

import com.aegis.core.dao.AegisDao;
import com.aegis.core.manager.AegisAppManager;
import com.aegis.core.manager.AegisComponentManager;
import com.aegis.core.model.AegisAppGroupPermission;
import com.aegis.core.utils.AegisCommonUtil;
import com.aegis.core.utils.AegisUserUtil;
import com.aegis.spring.config.AegisUserProperties;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author wuweixin
 *
 * @Version 1.0
 * @Descritube
 */
public class AegisInitializer {

    private List<AegisAppGroupPermission> groupPermissions;

    private String appId;

    public AegisInitializer() {

    }

    public AegisInitializer(String appId) {
        this.appId = appId;
    }

    @Autowired
    public void init(AegisDao redisAegisDao, AegisUserProperties aegisUserProperties) {
        if (AegisCommonUtil.isStringBlank(appId)) {
            this.appId = aegisUserProperties.getAppId();
        }
        AegisComponentManager.initAegisDao(redisAegisDao);
        if (!AegisCommonUtil.isStringBlank(appId)) {
            AegisAppManager.register(appId);
            AegisUserUtil.clearGroupPermission();
            AegisUserUtil.addGroupPermission(groupPermissions);
        }
    }


    /**
     * 初始化权限组
     *
     * @param groupPermissions
     */
    public AegisInitializer initGroupPermission(List<AegisAppGroupPermission> groupPermissions) {
        this.groupPermissions = groupPermissions;
        return this;
    }

}
